package org.summerframework.component.upload.fastdfs;

import org.summerframework.component.upload.exception.FastDFSException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.TrackerGroup;
import org.springframework.beans.factory.InitializingBean;

import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;

/**
 * FastDFS 初始化
 *
 * @author KisChang
 * @version 1.0
 */
public class FastDFSTemplateFactory extends GenericObjectPoolConfig implements InitializingBean {

    //connect_timeout
    private int connectTimeout;
    //network_timeout
    private int networkTimeout;
    //charset
    private String charset;
    //http.tracker_http_port
    private int trackerHttpPort;
    //http.anti_steal_token
    private boolean antiStealToken;
    //http.secret_key
    private String secretKey;

    private List<String> trackerServers;
    private TrackerGroup trackerGroup;

    private void setToGlobal() {
        ClientGlobal.setG_connect_timeout(this.connectTimeout);
        ClientGlobal.setG_network_timeout(this.networkTimeout);
        ClientGlobal.setG_charset(this.charset);
        ClientGlobal.setG_tracker_http_port(this.trackerHttpPort);
        ClientGlobal.setG_anti_steal_token(this.antiStealToken);
        ClientGlobal.setG_secret_key(this.secretKey);
        ClientGlobal.setG_tracker_group(this.trackerGroup);
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getNetworkTimeout() {
        return networkTimeout;
    }

    public void setNetworkTimeout(int networkTimeout) {
        this.networkTimeout = networkTimeout;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public int getTrackerHttpPort() {
        return trackerHttpPort;
    }

    public void setTrackerHttpPort(int trackerHttpPort) {
        this.trackerHttpPort = trackerHttpPort;
    }

    public boolean isAntiStealToken() {
        return antiStealToken;
    }

    public void setAntiStealToken(boolean antiStealToken) {
        this.antiStealToken = antiStealToken;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public List<String> getTrackerServers() {
        return trackerServers;
    }

    public void setTrackerServers(String trackerServers) {
        this.trackerServers = Arrays.asList(trackerServers.split(","));
    }

    public TrackerGroup getTrackerGroup() {
        return trackerGroup;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (connectTimeout <= 0) {
            connectTimeout = ClientGlobal.DEFAULT_CONNECT_TIMEOUT;
        }

        if (networkTimeout <= 0) {
            networkTimeout = ClientGlobal.DEFAULT_NETWORK_TIMEOUT;
        }
        connectTimeout *= 1000; //millisecond
        networkTimeout *= 1000; //millisecond

        if (charset == null || charset.length() == 0) {
            charset = "UTF-8";
        }

        if (trackerHttpPort <= 0) {
            trackerHttpPort = 80;
        }

        if (CollectionUtils.isEmpty(trackerServers)) {
            throw new FastDFSException("item \"tracker_server\"  not found");
        }

        InetSocketAddress[] trackerServersSocket = new InetSocketAddress[trackerServers.size()];

        for (int i = 0; i < trackerServers.size(); i++) {
            String str = trackerServers.get(i);
            String[] parts = str.split(":", 2);
            if (parts.length != 2) {
                throw new FastDFSException(
                        "the value of item \"tracker_server\" is invalid, the correct format is host:port");
            }

            trackerServersSocket[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
        }

        trackerGroup = new TrackerGroup(trackerServersSocket);

        if (antiStealToken) {
            if (secretKey == null || "".equals(secretKey)) {
                throw new FastDFSException("item \"secret_key\"  not found");
            }
        }

        setToGlobal();
    }
}
